package com.xiaoxu.spzx.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaoxu.spzx.model.dto.h5.ProductSkuDto;
import com.xiaoxu.spzx.model.entity.product.Product;
import com.xiaoxu.spzx.model.entity.product.ProductDetails;
import com.xiaoxu.spzx.model.entity.product.ProductSku;
import com.xiaoxu.spzx.model.vo.h5.ProductItemVo;
import com.xiaoxu.spzx.product.mapper.ProductDetailsMapper;
import com.xiaoxu.spzx.product.mapper.ProductMapper;
import com.xiaoxu.spzx.product.mapper.ProductSkuMapper;
import com.xiaoxu.spzx.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductSkuMapper productSkuMapper;

    // 接口实现类
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Override
    public List<ProductSku> selectProductSkuBysal() {
        return productSkuMapper.selectProductSkuBysal();
    }

    @Override
    public PageInfo<ProductSku> findByPage(Integer page,
                                           Integer limit,
                                           ProductSkuDto productSkuDto) {
        PageHelper.startPage(page,limit);
        List<ProductSku> productSkuList = productSkuMapper.findByPage(productSkuDto);
        return new PageInfo<>(productSkuList);
    }
    //商品详情

    /**
     * 根据SKU ID获取商品详情页面的信息
     *
     * @param skuId SKU的ID
     * @return ProductItemVo 商品详情页面展示的信息VO
     */
    @Override
    public ProductItemVo item(Long skuId) {

        // 获取指定ID的SKU信息
        ProductSku productSku = productSkuMapper.getById(skuId);

        // 根据SKU ID获取对应的商品信息
        Product product = productMapper.getById(productSku.getProductId());

        // 获取该商品下的所有SKU信息，并建立SKU规格与SKU ID的映射关系
        List<ProductSku> productSkuList = productSkuMapper.findByProductId(productSku.getProductId());
        Map<String, Object> skuSpecValueMap = new HashMap<>();
        productSkuList.forEach(item -> {
            skuSpecValueMap.put(item.getSkuSpec(), item.getId());
        });

        // 获取商品的详细信息
        ProductDetails productDetails = productDetailsMapper.getByProductId(productSku.getProductId());

        // 构建商品详情页面的VO对象
        ProductItemVo productItemVo = new ProductItemVo();

        // 设置SKU信息、商品信息、商品详情图片URL列表、商品滑动图片URL列表
        productItemVo.setProductSku(productSku);
        productItemVo.setProduct(product);
        productItemVo.setDetailsImageUrlList(Arrays.asList(productDetails.getImageUrls()
                .split(",")));
        productItemVo.setSliderUrlList(Arrays.asList(product.getSliderUrls()
                .split(",")));

        // 设置商品规格值列表、SKU规格与SKU ID的映射关系
        productItemVo.setSpecValueList(JSON.parseArray(product.getSpecValue()));
        productItemVo.setSkuSpecValueMap(skuSpecValueMap);

        return productItemVo;
    }
    //根据skuId查询sku信息
    @Override
    public ProductSku findSkuById(Long skuId) {
        ProductSku proSku = productSkuMapper.getById(skuId);
        return proSku;
    }

}
